/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chessboard;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsConfiguration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.j3d.Alpha;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.J3DGraphics2D;
import javax.vecmath.Color3f;

/**
 *
 * @author Jedrek
 */
public class MyCanvas3D extends Canvas3D implements Runnable{

    public String text;
    public Color3f blackoutColor;
    public Color3f textColor;
    public float transparency;
    public boolean isBlackout;
    public Font font;

    public float transparencyValue = 0f;
    public float animationSpeed = 0.03f;
    Thread thread = null;

    public MyCanvas3D(GraphicsConfiguration conf){
        super(conf);
        text = new String();
        isBlackout = false;
        blackoutColor = new Color3f(1f, 1f, 1f);
        textColor = new Color3f(0f, 0f, 0f);
        transparency = 0.7f;
        font = new Font("Times New Roman", Font.BOLD, 50);
    }
    
    @Override
    public void postRender(){
        if(!isBlackout)
            return;
        
        int width = getWidth();
        int height = getHeight();
        J3DGraphics2D g = getGraphics2D();

        g.flush(true);
        g.setColor(new Color(blackoutColor.x, blackoutColor.y, blackoutColor.z, transparency * transparencyValue));
        g.fillRect(0, 0, width, height);
        g.setColor(new Color(textColor.x, textColor.y, textColor.z, transparencyValue));
        g.setFont(font);
        FontMetrics metrics = g.getFontMetrics(font);
        int hgt = metrics.getHeight();
        int adv = metrics.stringWidth(text);
        g.drawString(text, (width-adv)/2, width/2-hgt);
    }

    public void startAnim(){
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        transparencyValue = 0f;
        do{
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
            }
            transparencyValue += (1.15f-transparencyValue)*animationSpeed;
            
            if(transparencyValue>1f)
                transparencyValue = 1f;
            getGraphicsContext3D().clear();
        } while(transparencyValue<1f);
    }
}
